package com.example.lock.controller;

import com.alibaba.fastjson2.JSON;
import com.example.lock.model.Hobby;
import com.example.lock.model.Student;
import com.example.lock.service.StudentService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>Description: </p>
 *
 * @author zg
 * @version v1.0.0
 * @since 2024/8/19
 **/
@Slf4j
@RequiredArgsConstructor
@RestController
@RequestMapping("/student/")
public class StudentController {

    private final StudentService studentService;

    @GetMapping("list")
    public List<Student> doFindStu() {
        log.info("查询学生列表：");
        List<Student> list = studentService.list();
        for (Student stu : list) {
            log.info("id={}, name={}, gender={}, cttime={}, uptime={}", stu.getId(), stu.getName(), stu.getGender(), stu.getCttime(), stu.getUptime());
        }
        return list;
    }

    @PostMapping("add/submit")
    public void addSubmit(@RequestBody Student stu) {
        log.info("添加学生信息：{}", JSON.toJSONString(stu));
        studentService.add(stu);
    }

    @DeleteMapping("delete/{sid}")
    public void doFindStu(@PathVariable Long sid) {
        log.info("删除学生信息：id={}", sid);
        studentService.delete(sid);
    }

    @PutMapping("update/{sid}")
    public void update(@PathVariable Long sid) {
        Student stu = studentService.getBySid(sid);
    }

    @PutMapping("update/submit")
    public void updateSubmit(@RequestBody Student stu) {
        log.info("更新学生信息：{}", JSON.toJSONString(stu));
        studentService.updateBySid(stu);
    }

    @PutMapping("update/submit/{hobbyName}")
    public void updateSubmitWithHobby(@RequestBody Student stu, @PathVariable String hobbyName) {
        log.info("更新学生信息：{},hobbyName={}", JSON.toJSONString(stu), hobbyName);
        studentService.updateBySidAndHobby(stu, new Hobby(hobbyName));
    }

}
